//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Helpers;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Data;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Oracle;
#endregion

namespace Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle
{
    internal class OracleViewTranslator
    {
        #region Public Implementation
        public OracleView OracleViewCollectionToView(DataRow oracleView, DataTable oracleColumnsColletions)
        {
            OracleView view = new OracleView();

            view.Name = oracleView["view_name"].ToString();
            view.Schema = oracleView["owner"].ToString();

            foreach(DataRow row in oracleColumnsColletions.Rows)
            {
                OracleColumn column = new OracleColumn();
                OracleDbDataType dbDataType = new OracleDbDataType();

                column.Name = row["Column_Name"].ToString();
                column.IsNullable = OracleTranslatorHelper.IsNullable(row["nullable"].ToString());

                dbDataType.ProviderType = OracleDataTypeConverter.String2DatabaseType(row["DataType"].ToString());
                dbDataType.Type = row["DataType"].GetType();
                dbDataType.Size = OracleTranslatorHelper.GetSize(row["length"]);
                dbDataType.Precision = OracleTranslatorHelper.GetPrecision(row["Precision"]);
                dbDataType.Scale = OracleTranslatorHelper.GetScale(row["Scale"]);

                column.DbDataType = dbDataType;
                view.Columns.Add(column);
            }

            return view;
        } 
        #endregion
    }
}